// pages/group/dynamicdetail/dynamicdetail.js
import {
    _get,
    _headerGet,
    _headerPost,
    config
} from '../../../utils/httpUtils.js';
const RSA = require("../../../utils/wx_rsa.js");
const app = getApp();
Page({
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '详情' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        userName: '',
        showComment: true, //显示表单
        focus: false, //聚焦输入框
        bottom: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.getSystemInfo({ //获取系统信息
            success: (res) => {
                this.setData({
                    windowHeight: res.screenHeight - this.data.height
                })
            }
        })
        let that = this;
        if (options.id) { // 如果有查询 id 执行
            that.getDetail(options.id)
            that.data.id = options.id
        } else { //没有查询id, 取缓存数据

            wx.getStorage({
                key: 'userdynamic',
                success: function(res) {
                    that.setData({
                        dynamic: res.data
                    })
                },
            })
        }

        wx.getStorage({ // 取出登录缓存的用户昵称
            key: 'token',
            success: function(res) {
                that.data.userName = res.data.nickname
            },
        })
    },

    getDetail(_id) {
        _headerGet('/api/Dynamic/detail', {
            'id': _id
        }, res => {
            // console.log(res.data)
            if (res.data.code == 1) {
                this.setData({
                    dynamic: res.data.data
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },
    follow(e) { //关注 
        let id = e.currentTarget.dataset.id;
        _headerPost("/api/user/add_attention", {
            'by_uid': RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${id}`)),
            'c_client': RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`weixinSmall`))
        }, res => {
            if (res.data.code == 1) {
                this.data.dynamic.dynamic_is_attention = 1;
                this.setData({
                    dynamic: this.data.dynamic
                })
                wx.showToast({
                    title: res.data.msg,
                    duration: 2000,
                    icon: 'success'
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    duration: 2000,
                    icon: 'none'
                })
            }
        })
    },
    // 取消关注
    removeconc(e) {
        let _data = {
            c_client: "weixinSmall",
            user_id: this.data.dynamic.user_uid
        }
        _headerPost('/api/user/del_attention', _data, res => {
            if (res.data.code == 1) {
                this.data.dynamic.dynamic_is_attention = 0;
                this.setData({
                    dynamic: this.data.dynamic
                })
                wx.showToast({
                    title: res.data.msg,
                    duration: 2000,
                    icon: 'success'
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    duration: 2000,
                    icon: 'none'
                })
            }
        })
    },
    previewImage(e) { //图片预览
        let currentSrc = e.currentTarget.dataset.src;
        let currentSrcArray = e.currentTarget.dataset.srcarr;
        wx.previewImage({
            current: currentSrc, // 当前显示图片的http链接
            urls: currentSrcArray // 需要预览的图片http链接列表
        })
    },
    likes(e) { //点赞
        let _isZan = this.data.dynamic.dynamic_is_zan;
        let _data = {
            'type': 2,
            'id': e.currentTarget.dataset.id
        }
        if (_isZan == 1) {
            this.data.dynamic.dynamic_is_zan = 0
            this.data.dynamic.dynamic_zan_count--
        } else if (_isZan == 0) {
            this.data.dynamic.dynamic_is_zan = 1
            this.data.dynamic.dynamic_zan_count++
        }
        this.like_zan(_data)
    },
    like_zan(data) { //执行点赞
        _headerPost('/api/zan/zan', data, res => {
            if (res.data.code == 1) {
                this.setData({
                    dynamic: this.data.dynamic
                })
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }

        })
    },
    outcomment(e) { //调出评论输入框
        this.setData({
            showComment: false,
            focus: true,
            _data: {
                'ac_type': 1,
                'id': e.currentTarget.dataset.acid,
                'key': e.currentTarget.dataset.index
            }
        })
    },

    comment(e) { //发布评论
        let data = this.data._data;
        data.content = e.detail.value.content
        // console.log(data)
        let key = data.key
        let comment = {
            content: data.content,
            u_nickname: this.data.userName
        }
        _headerPost('/api/comment/addComment', data, res => {
            // console.log(res.data)
            if (res.data.code == 1) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 1000
                })
                this.data.dynamic.dynamic_comment_list.push(comment) //模拟假数据实时添加
                this.setData({
                    showComment: true,
                    tmpStorage: '', //置空评论框
                    dynamic: this.data.dynamic //数据重载
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    duration: 2000,
                    icon: 'none'
                })
            }
        })
    },

    bindblur(e) { //评论失焦
        this.setData({
            showComment: true,
            focus: false,
            bottom: 0
        })
    },

    bindfocus(e) {
        this.setData({ //评论聚焦
            bottom: e.detail.height
        })
    },

    onPullDownRefresh: function() {
        this.getDetail(this.data.id)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },
    onShareAppMessage: function() {
        return {
            title: `${this.data.dynamic.dynamic_content}`
        }
    }
})